using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// A <see cref="IBusinessObjectRule"/> that validates that a <see cref="DMClass"/>
    /// has at least one <see cref="DMProperty"/>
    /// </summary>
    public class DMClassMustHaveOneProp : IBusinessObjectRule
    {
        private DMClass _dmClass;

//        public DMClassMustHaveOneProp(DMClass dmClass)
//        {
//            if (dmClass == null) throw new ArgumentNullException("dmClass");
//            _dmClass = dmClass;
//        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmClass = (DMClass) bo;
            if (_dmClass.Properties.Count > 0) return true;
            if (_dmClass.InheritanceSuperClassRelationship == null) return false;
            return _dmClass.InheritanceSuperClassRelationship.SuperClass != null;
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "ClassMustHaveOneProp"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get { return "The Entity/Class '" + _dmClass + "' must have at least one Property"; }
        }
        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Warning; }
        }

        #endregion
    }
}